package com.zhiche.lisa.integration.controller;

import com.alibaba.fastjson.JSON;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.enums.SystemEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.redislog.RedisLogUtil;
import com.zhiche.lisa.integration.dto.carrier.CarrierErpDto;
import com.zhiche.lisa.integration.dto.carrier.DriverErpDto;
import com.zhiche.lisa.integration.dto.carrier.SupplierErpDto;
import com.zhiche.lisa.integration.dto.carrier.TrailerErpDto;
import com.zhiche.lisa.integration.inteface.erp.ErpShippingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 对接ERP 接口
 * <p>
 * 1.获取车头信息
 * 2.获取挂车信息
 * 3.获取司机信息
 * </p>
 */
@RestController
@RequestMapping("/erp-api")
@Api(value = "erp-api", description = "供ERP的接口", tags = {"提供给第三方的接口"})
public class ErpAPIController {

    @Autowired
    private ErpShippingService erpShippingService;

    @Value("${logRedis.isTest}")
    private boolean isTest;

    @PostMapping(value = "/carrierPushErp")
    @ApiOperation(value = "牵引车（运力平台推送OTM，自有车队推送ERP）", notes = "牵引车（运力平台推送OTM，自有车队推送ERP）",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse carrierPushErp(@RequestBody CarrierErpDto carrierErpDto) {
        RestfulResponse<Object> response = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "成功", null);
        try {
            RedisLogUtil.addSysLogs(SystemEnum.LSPM.getCode(),SystemEnum.INTEGRATION.getCode(),"carrier"+carrierErpDto.getId(), JSON.toJSONString(carrierErpDto),"/erp-api/carrierPushErp",2,1,null,isTest);
            erpShippingService.carrierPushErp(carrierErpDto);
        } catch (BaseException be) {
            RedisLogUtil.addSysLogs(SystemEnum.LSPM.getCode(),SystemEnum.INTEGRATION.getCode(),"carrier"+carrierErpDto.getId(), JSON.toJSONString(carrierErpDto),"/erp-api/carrierPushErp",3,0,be.getMessage(),isTest);
            return new RestfulResponse<>(CommonEnum.ERROR.getCode(), be.getMessage(), null);
        } catch (Exception ex) {
            RedisLogUtil.addSysLogs(SystemEnum.LSPM.getCode(),SystemEnum.INTEGRATION.getCode(),"carrier"+carrierErpDto.getId(), JSON.toJSONString(carrierErpDto),"/erp-api/carrierPushErp",3,0,ex.getMessage(),isTest);
            return new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return response;
    }

    @PostMapping(value = "/trailerPushErp")
    @ApiOperation(value = "挂车数据同步", notes = "挂车数据同步",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse trailerPushErp(@RequestBody TrailerErpDto trailerErpDto) {
        RestfulResponse<Object> response = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "成功", null);
        try {
            RedisLogUtil.addSysLogs(SystemEnum.LSPM.getCode(),SystemEnum.INTEGRATION.getCode(),"trailer"+trailerErpDto.getId(), JSON.toJSONString(trailerErpDto),"/erp-api/trailerPushErp",2,1,null,isTest);
            erpShippingService.trailerPushErp(trailerErpDto);
        } catch (BaseException be) {
            RedisLogUtil.addSysLogs(SystemEnum.LSPM.getCode(),SystemEnum.INTEGRATION.getCode(),"trailer"+trailerErpDto.getId(), JSON.toJSONString(trailerErpDto),"/erp-api/trailerPushErp",3,0,be.getMessage(),isTest);
            return new RestfulResponse<>(CommonEnum.ERROR.getCode(), be.getMessage(), null);
        } catch (Exception ex) {
            RedisLogUtil.addSysLogs(SystemEnum.LSPM.getCode(),SystemEnum.INTEGRATION.getCode(),"trailer"+trailerErpDto.getId(), JSON.toJSONString(trailerErpDto),"/erp-api/trailerPushErp",3,0,ex.getMessage(),isTest);
            return new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return response;
    }

    @PostMapping(value = "/driverPushErp")
    @ApiOperation(value = "司机数据同步", notes = "司机数据同步",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse driverPushErp(@RequestBody DriverErpDto driverErpDto) {
        RestfulResponse<Object> response = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "成功", null);
        try {
            RedisLogUtil.addSysLogs(SystemEnum.LSPM.getCode(),SystemEnum.INTEGRATION.getCode(),"driver"+driverErpDto.getId(), JSON.toJSONString(driverErpDto),"/erp-api/driverPushErp",2,1,null,isTest);
            erpShippingService.driverPushErp(driverErpDto);
        } catch (BaseException be) {
            RedisLogUtil.addSysLogs(SystemEnum.LSPM.getCode(),SystemEnum.INTEGRATION.getCode(),"driver"+driverErpDto.getId(), JSON.toJSONString(driverErpDto),"/erp-api/driverPushErp",3,0,be.getMessage(),isTest);
            return new RestfulResponse<>(CommonEnum.ERROR.getCode(), be.getMessage(), null);
        } catch (Exception ex) {
            RedisLogUtil.addSysLogs(SystemEnum.LSPM.getCode(),SystemEnum.INTEGRATION.getCode(),"driver"+driverErpDto.getId(), JSON.toJSONString(driverErpDto),"/erp-api/driverPushErp",3,0,ex.getMessage(),isTest);
            return new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return response;
    }

    @PostMapping(value = "/supplierPushErp")
    @ApiOperation(value = "供方（车队）数据同步", notes = "供方（车队）数据同步",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse supplierPushErp(@RequestBody SupplierErpDto supplierErpDto) {
        RestfulResponse<Object> response = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "成功", null);
        try {
            RedisLogUtil.addSysLogs(SystemEnum.LSPM.getCode(),SystemEnum.INTEGRATION.getCode(),"fleet"+supplierErpDto.getId(), JSON.toJSONString(supplierErpDto),"/erp-api/supplierPushErp",2,1,null,isTest);
            erpShippingService.supplierPushErp(supplierErpDto);
        } catch (BaseException be) {
            RedisLogUtil.addSysLogs(SystemEnum.LSPM.getCode(),SystemEnum.INTEGRATION.getCode(),"fleet"+supplierErpDto.getId(), JSON.toJSONString(supplierErpDto),"/erp-api/supplierPushErp",3,0,be.getMessage(),isTest);
            return new RestfulResponse<>(CommonEnum.ERROR.getCode(), be.getMessage(), null);
        } catch (Exception ex) {
            RedisLogUtil.addSysLogs(SystemEnum.LSPM.getCode(),SystemEnum.INTEGRATION.getCode(),"fleet"+supplierErpDto.getId(), JSON.toJSONString(supplierErpDto),"/erp-api/supplierPushErp",3,0,ex.getMessage(),isTest);
            return new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return response;
    }
}
